package com.glory.dgcpcpda.activity.mfg;

import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.glory.dgcpcpda.base.MyApp;
import androidx.recyclerview.widget.RecyclerView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.adapter.StationDtoAdapter;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.base.Constant;
import com.glory.dgcpcpda.base.MyApp;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.EqpInfoEntity;
import com.glory.dgcpcpda.entity.LotInfoEntity;
import com.glory.dgcpcpda.entity.PdaSetEntity;
import com.glory.dgcpcpda.entity.StationDtoEntity;
import com.glory.dgcpcpda.entity.ToolCheckEntity;
import com.glory.dgcpcpda.entity.ToolEntity;
import com.glory.dgcpcpda.entity.UpdateIPEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.EditTextEnterUtils;
import com.glory.dgcpcpda.utils.LotUtils;
import com.glory.dgcpcpda.utils.NetUtils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_TextCallback;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog;
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralPop;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_PopUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_SharePreferencesUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StaticMembers;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONObject;

import java.text.Format;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ToolCheckActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    //盘点单号
    @BindView(R.id.tvToolCheckID)
    FreeUI_ClearEditText tvToolCheckID;
    //类型-治具
    @BindView(R.id.rbTooling)
    RadioButton rbTooling;
    //类型-物料
    @BindView(R.id.rbMaterial)
    RadioButton rbMaterial;
    //物料批号
    @BindView(R.id.etToolID)
    FreeUI_ClearEditText etToolID;
    //数量
    @BindView(R.id.etNum)
    FreeUI_ClearEditText etNum;
    @BindView(R.id.llMain)
    LinearLayout llMain;
    //提交按钮
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    private String mType = "";

    private FreeUI_GeneralFragmentDialog dialog;


    @Override
    protected int getLayoutResId() {
        return R.layout.activity_tool_check;
    }

    @Override
    protected void initView() {
        tvTitle.setText("物料盘点");
        tvBaseSubmit.setText("提交");
        mType = "Material";
        Constant.TOOL_CHECK_ID = FreeApi_SharePreferencesUtils.getSharePre("DG_CPC_FILE", "TOOL_CHECK_ID", Constant.TOOL_CHECK_ID, MyApp.app);
        tvToolCheckID.setText(Constant.TOOL_CHECK_ID);
        setMainLayout(llMain);


        initEdit(tvToolCheckID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                tvToolCheckID.setText(s);
                Constant.TOOL_CHECK_ID = s;
                showToast("盘点单号保存成功!",true);
                FreeApi_SharePreferencesUtils.setSharePre("DG_CPC_FILE","TOOL_CHECK_ID",s,MyApp.app);
            }
        });


        initEdit(etToolID, new editTextListener() {
            @Override
            public void onTextChangeListener(String s) {
                FreeUI_EntityCallBack<BaseResponseBean<ToolCheckEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<ToolCheckEntity>>
                        (new TypeToken<BaseResponseBean<ToolCheckEntity>>() {
                        }.getType(), getSupportFragmentManager(), this){
                    @Override
                    public  void onSuccess(Response<BaseResponseBean<ToolCheckEntity>> response){
                        super.onSuccess(response);
                        if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                            ToolCheckEntity body = response.body().getResponse().getBody();
                            if(mType == "Material"){
                                etNum.setText(body.getXMLot().getMAINQTY());
                            }else {
                                etNum.setText(body.getXTool().getMAINQTY());
                            }
                        }
                    }
                    @Override
                    public void onError(Response<BaseResponseBean<ToolCheckEntity>> response) {
                        super.onError(response);
                        loadError(response.getException(), "TRANSFERMES");
                    }
                };
                JsonObject map =new JsonObject();
                map.addProperty("MLOTID",s);
                map.addProperty("ACTIONTYPE","QUERYBYID");
                map.addProperty("MATERIALTYPE",mType);
                NetUtils.requestPostNet("CPC.INVENTORYFINDMLOTBYID","TRANSFERMES",ToolCheckActivity.this,map,callBack);
            }
        });

    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.tvBaseSubmit,R.id.rbTooling,R.id.rbMaterial})
    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.rbTooling:
                mType = "Tooling";
                etToolID.setText("");
                etNum.setText("");
                break;
            case R.id.rbMaterial:
                mType = "Material";
                etToolID.setText("");
                etNum.setText("");
                break;
            case  R.id.tvBaseSubmit:
                if(getText(tvToolCheckID).equals("")){
                    showToast("盘点单号不能为空",false);
                    return;
                }
                if(getText(etToolID).equals("")){
                    showToast("治具或物料批号不能为空",false);
                    return;
                }
                try {
                    if(getText(etNum).equals("")){
                        showToast("数量不能为空",false);
                        return;
                    }
                    Float.parseFloat(getText(etNum));
                }catch (NumberFormatException e){
                    showToast("数量栏请输入数字", false);
                    return;
                }
                showSubmitDialog(new onSubmitListener() {
                    @Override
                    public void setOnSubmitListener() {
                        FreeUI_EntityCallBack<BaseResponseBean<ToolCheckEntity>> callBack = new FreeUI_DialogEntityCallBack<BaseResponseBean<ToolCheckEntity>>
                                (new TypeToken<BaseResponseBean<ToolCheckEntity>>() {
                                }.getType(), getSupportFragmentManager(), this) {

                            @Override
                            public void onSuccess(Response<BaseResponseBean<ToolCheckEntity>> response) {
                                super.onSuccess(response);
                                if (response.body().isSuccess(getApplicationContext(),false,getSupportFragmentManager())){
                                    showConfirmDialog("提交成功");
                                    etToolID.setText("");
                                    etNum.setText("");
                                }
                            }

                            @Override
                            public void onError(Response<BaseResponseBean<ToolCheckEntity>> response) {
                                super.onError(response);
                                loadError(response.getException(),"TRANSFERMES");

                            }
                        };
                        JsonObject map = new JsonObject();
                        map.addProperty("ACTIONTYPE","SUBMITBYID");
                        map.addProperty("MLOTID",etToolID.getText().toString());
                        map.addProperty("MATERIALTYPE",mType);
                        float num = Float.parseFloat(etNum.getText().toString());
                        map.addProperty("MAINQTY",num);
                        map.addProperty("TOOLCHECKID",tvToolCheckID.getText().toString());
                        NetUtils.requestPostNet("CPC.INVENTORYFINDMLOTBYID","TRANSFERMES",this,map,callBack);
                    }
                });
        }
    }
}
